package com.example.task1.ui.AllService.parking;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.base.BaseActivity;
import com.example.task1.bean.ParkingHomeDetailBean;
import com.example.task1.bean.ParkingLotBean;
import com.example.task1.http.Api;
import com.example.task1.ui.AllService.EventManagement.EventCommentAdapter;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import java.util.HashMap;

import retrofit2.Call;

public class ParkingHomeDetailsActivity extends BaseActivity {

    private int id;
    private TextView tvName;
    private ImageView image;
    private TextView tvAddress;
    private TextView tvRates;
    private TextView tvPriceCaps;
    private TextView tvVacancy;
    private TextView tvDistance;
    private RecyclerView rvList;
    private Button btnSubMit;
    private int pageNum = 1;
    private boolean isTrue = true;
    private LinearLayoutManager linearLayoutManager;
    private ParkingLotAdapter parkingLotAdapter;
    private ParkingHomeDetailBean.DataDTO date;

    private void loadCommentList() {
        rvList.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                if (parkingLotAdapter.getItemCount() - 1 == linearLayoutManager.findLastVisibleItemPosition() && isTrue) {
                    isTrue = false;
                    getParkingLot(date.getParkName());
                }
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_parking_home_details);
        id = getIntent().getIntExtra("id", 0);
        initView();
        setAdapter();
        initData();
        loadCommentList();
        btnSubMit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(ParkingHomeDetailsActivity.this);
                EditText editText = new EditText(ParkingHomeDetailsActivity.this);
                editText.setHint("请输入纠错信息");
                builder.setView(editText);
                builder.setNegativeButton("提交", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String text = editText.getText().toString();
                        if (TextUtils.isEmpty(text)) {
                            MyApplication.show("内容不能为空");
                        } else {
                            HashMap<String, Object> map = new HashMap<>();
                            map.put("name", date.getParkName());
                            map.put("content", text);
                            Call<ApiResp> post = Api.api().post(MyApplication.TOKEN, "/prod-api/api/park/correct", Api.getBody(map));
                            Api.send(post, ApiResp.class, new CallBack<ApiResp>() {
                                @Override
                                public void success(ApiResp data) {
                                    MyApplication.show("提交成功");
                                }

                                @Override
                                public void error(String msg) {

                                }
                            });
                        }
                    }
                });
                builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.show();
            }
        });
    }

    private void setAdapter() {
        parkingLotAdapter = new ParkingLotAdapter(this);
        linearLayoutManager = new LinearLayoutManager(this);
        rvList.setAdapter(parkingLotAdapter);
        rvList.setLayoutManager(linearLayoutManager);
    }

    private void initData() {
        Call<ApiResp> apiRespCall = Api.api().get(MyApplication.TOKEN, "/prod-api/api/park/lot/" + id);
        Api.send(apiRespCall, ParkingHomeDetailBean.class, new CallBack<ParkingHomeDetailBean>() {
            @Override
            public void success(ParkingHomeDetailBean data) {
                date = data.getData();
                MyApplication.glide(image, date.getImgUrl());
                tvAddress.setText("地址：" + date.getAddress());
                tvRates.setText("收费：" + date.getRates());
                tvPriceCaps.setText("封顶价格：" + date.getPriceCaps());
                tvVacancy.setText("空位个数：" + date.getVacancy());
                tvDistance.setText("距离：" + date.getDistance());
                tvName.setText(date.getParkName());
                getParkingLot(date.getParkName());
            }

            @Override
            public void error(String msg) {

            }
        });
    }

    private void getParkingLot(String parkName) {
        Call<ApiResp> call = Api.api().get(MyApplication.TOKEN, "/prod-api/api/park/lot/record/list?parkName=" + parkName + "&pageSize=10&pageNum=" + pageNum);
        Api.send(call, ParkingLotBean.class, new CallBack<ParkingLotBean>() {
            @Override
            public void success(ParkingLotBean data) {
                if (parkingLotAdapter.getData().size() < data.getTotal()) {
                    pageNum++;
                    isTrue = true;
                    parkingLotAdapter.setData(data.getRows());
                } else {
                    MyApplication.show("没有更多数据了");
                }
            }

            @Override
            public void error(String msg) {

            }
        });
    }

    @Override
    public String getMyTitle() {
        return "停车场详情";
    }

    private void initView() {
        tvName = findViewById(R.id.tv_name);
        image = findViewById(R.id.image);
        tvAddress = findViewById(R.id.tv_address);
        tvRates = findViewById(R.id.tv_rates);
        tvPriceCaps = findViewById(R.id.tv_priceCaps);
        tvVacancy = findViewById(R.id.tv_vacancy);
        tvDistance = findViewById(R.id.tv_distance);
        rvList = findViewById(R.id.rv_list);
        btnSubMit = findViewById(R.id.btn_subMit);
    }
}